//
//  GHUtils.m
//  GodHorses
//
//  Created by Mac on 2017/11/14.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "TMUtils.h"

@implementation TMUtils
+ (void)netWorkMonitorinCanUseWithHandle:(void (^)())handle{
    
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {
            if (handle) {
                handle();
            }
        }
    }];
    [manager startMonitoring];
}
+ (UIView*)headerViewWithTitle:(NSString*)title{
    UIView * sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 44)];
    sectionHeader.backgroundColor = [UIColor whiteColor];
    STButton * line = [[STButton alloc] initWithFrame:CGRectMake(15, 0, 4, 22)
                                                title:nil
                                           titleColor:nil
                                            titleFont:14
                                         cornerRadius:0
                                      backgroundColor:TM_firstTextColor
                                      backgroundImage:nil
                                                image:nil];
    line.centerY = 22;
    [sectionHeader addSubview:line];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(line.right + 10, 0, 200, 44)
                                                     text:title
                                                textColor:TM_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    
    [sectionHeader addSubview:titleLable];
    return sectionHeader;
}
+ (UINavigationBar *)navigationBar{
    UINavigationController* nav = (id)[UIApplication sharedApplication].keyWindow.rootViewController.childViewControllers.firstObject;
    return nav.navigationBar;
}
+  (UITabBar *)tabbar{
    UITabBarController* tab = (id)[UIApplication sharedApplication].keyWindow.rootViewController;
    return tab.tabBar;
}
+ (CGFloat)navgationBarBootom{
    if (KIsiPhoneX) {
        return 88;
    }
    return 64;
}
+ (CGFloat)tabBarTop{
    if (KIsiPhoneX) {
        return (UIScreenHeight - 83);
    }
    return (UIScreenHeight - 49);
}
+ (void)autoLogin{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    BBUserModel * user = [STUserManger defult].loginedUser;
    if (user.openid.length) {
        [self autoLoginWithOpenId:user.openid];
        return;
    }
    if (user.telephone.description.length) {
        [dic setObject:[STUserManger defult].loginedUser.telephone forKey:@"phone"];
    }else{
        NSLog(@"telephone为空");
        return;
    }
    if (user.password.description.length) {
        [dic setObject:[STUserManger defult].loginedUser.password forKey:@"password"];
    }else{
        NSLog(@"password为空");
        return;
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",severHeader,@"/api/user/login"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypeGet
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 
                                                 NSDictionary * modelDIc = [responseObject valueForKey:@"user"];
                                                 BBUserModel * user = [BBUserModel mj_objectWithKeyValues:modelDIc];
                                                 [[STUserManger defult] updateUserModel:user];
                                                 NSLog(@"自动登录成功");
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                //
                                                 NSLog(@"自动登录失败 ，只尝试自动 登录一次");
                                                 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                                     static dispatch_once_t onceToken;
                                                     dispatch_once(&onceToken, ^{
                                                        [TMUtils autoLogin];
                                                     });
                                                 });
                                                 [SVProgressHUD dismiss];
                                                 // [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
+ (void)autoLoginWithOpenId:(NSString*)openID{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if (openID.length) {
        [dic setObject:openID forKey:@"openid"];
         [dic setObject:openID forKey:@"nickname"];
         [dic setObject:openID forKey:@"headimgurl"];
    }else{
        NSLog(@"openid为空");
        return;
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",severHeader,@"/api/user/otherLogin"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypeGet
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * modelDIc = [responseObject valueForKey:@"user"];
                                                 BBUserModel * user = [BBUserModel mj_objectWithKeyValues:modelDIc];
                                                 user.openid = openID;
                                                 [[STUserManger defult] updateUserModel:user];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
+ (void)sendUserInfoRequest:(void(^)(BBUserModel * user))handle{
    
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([STUserManger defult].loginedUser.userID.description.length) {
        [dic setObject:[STUserManger defult].loginedUser.userID forKey:@"uid"];
    }else{
        NSLog(@"userID为空");
        if (handle) {
            handle(nil);
        }
        return;
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",severHeader,@"/api/user/getUserInfo"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypeGet
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {


                                                 NSDictionary * modelDIc = [responseObject valueForKey:@"user"];
                                                 BBUserModel * user = [BBUserModel mj_objectWithKeyValues:modelDIc];
                                                 [[STUserManger defult] updateUserModel:user];
                                                 if (handle) {
                                                     handle(user);
                                                 }
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 if (handle) {
                                                     handle(nil);
                                                 }
                                                 [SVProgressHUD dismiss];
                                                // [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
+ (void)sendGetShareAwardRequest{
    NSLog(@"---获取分享奖励请求开始");
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * shareID = [BBSystemManger manger].shareNewsID;
    if (shareID.description.length) {
        [dic setObject:shareID forKey:@"id"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",severHeader,@"/api/user/shareAward"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypeGet
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 NSString * count = [responseObject[@"count"] description];
                                                 NSString * coin = [responseObject[@"coin"] description];
                                                 NSLog(@"---获取分享奖励请求成功");
                                                  NSLog(@"---获取分享奖励展示提示成功");
                                                 [TMUtils showViewFormArard:[NSString stringWithFormat:@"+ %@",coin] num:count];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                  NSLog(@"---获取分享奖励请求失败");
                                             }];
}
+ (void)showViewFormArard:(NSString*)award num:(NSString*)num{
    UIWindow * window = [UIApplication sharedApplication].delegate.window;
    
    UIView * aleartView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 130, 160)];
    aleartView.backgroundColor = [UIColor whiteColor];
    //aleartView.layer.cornerRadius = 5;
   // aleartView.clipsToBounds = YES;
    [aleartView st_showRightAndBottomShadow];
    aleartView.center = window.center;
    [window addSubview:aleartView];
    
    STButton * iconButton = [[STButton alloc] initWithFrame:CGRectMake(0, 20, 40, 40)
                                                     title:nil
                                                titleColor:nil
                                                 titleFont:0
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:[UIImage imageNamed:@"挖矿"]];
    iconButton.centerX = aleartView.width / 2;
    [aleartView addSubview:iconButton];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, iconButton.bottom + 10, aleartView.width, 40)
                                                     text:award
                                                textColor:FirstTextColor
                                                     font:20
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    [aleartView addSubview:titleLable];
   
    
    STButton * todayButton = [[STButton alloc] initWithFrame:CGRectMake(0, titleLable.bottom, aleartView.width, 30)
                                                      title:[NSString stringWithFormat:@"今日已经领取 %@ 次 >",num]
                                                 titleColor:SecendTextColor
                                                  titleFont:12
                                               cornerRadius:0
                                            backgroundColor:nil
                                            backgroundImage:nil
                                                      image:nil];
    iconButton.centerX = aleartView.width / 2;
    todayButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [aleartView addSubview:todayButton];
    
//    return;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.25 animations:^{
            aleartView.alpha = 0.1;
        }completion:^(BOOL finished) {
            [aleartView removeFromSuperview];
        }];
    });
    
}
+ (void)sendGetLookAwardRequest{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",severHeader,@"/api/user/readAward"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypeGet
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 
                                             }];
}
@end
